﻿namespace Moe.ECamel.Common.Entities
{
	using System;
	using System.ComponentModel;

	public partial class Rental : IEditableObject
	{
		public String ItemTypeName
		{
			get { return this.Inventory.Item.ItemType.ItemTypeName; }
		}

		public String ItemTitle
		{
			get { return this.Inventory.Item.Title; }
		}

		public String ItemCatalogNumber
		{
			get { return this.Inventory.Item.CatalogNumber; }
		}

		public int ItemTypeId
		{
			get { return this.Inventory.Item.ItemTypeId; }
		}

		public ECamelError ValidateFinePayment()
		{
			// Validate rental has been returned:
			if (this.ActualReturnDate == null)
				return new ECamelError(MessageIds.RentalFineInvalidNotReturnedYet);
			// Validate rental has a fine to pay:
			if (this.Fine == 0)
				return new ECamelError(MessageIds.RentalFineInvalidNoFineToPay);
			// Validate rental fine has not been payed yet:
			if (this.FinePaymentId != null)
				return new ECamelError(MessageIds.RentalFineInvalidAlreadyPayed);

			return null;
		}

		private Rental _originalEntity = null;

		public void BeginEdit()
		{
			if (this._originalEntity == null)
			{
				this._originalEntity = new Rental();
				this._originalEntity._Active = this._Active;
				this._originalEntity._ActualReturnDate = this._ActualReturnDate;
			}
		}

		public void EndEdit()
		{
			if (this._originalEntity != null)
			{
				this._Active = this._originalEntity._Active;
				this._ActualReturnDate = this._originalEntity._ActualReturnDate;
			}
		}

		public void CancelEdit()
		{
			this._originalEntity = null;
		}
	}
}
